package com.zhufei.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.zhufei.entity.Introduction;

public class IntroductionDao {
	private static String TABLE = "zhufei_introduction";
	private static String ID = "ID";
	private static String IMAGE_PATH = "image_path";
	private static String CONTENT = "content";
	
	/**
	 * 修改简介内容
	 * @param id   简介id
	 * @param image_path  简介图片路径
	 * @param content   简介内容
	 * @return
	 */
	public static boolean updateIntroduction(String id, String image_path, String content){
		DBCon con = new DBCon();
		try {
			Statement statement = con.getStatement();
			String sql = "update "+TABLE+" set "+IMAGE_PATH+"='"+image_path+"',"+CONTENT+"='"+content+"' where "+ID+"='"+id+"'";
			statement.executeUpdate(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return false;
	}
	
	/**
	 * 获取数据库中唯一一条简介记录
	 * @return
	 */
	public static Introduction getIntroduction(){
		DBCon con = new DBCon();
		try {
			Statement statement = con.getStatement();
			String sql = "select * from "+TABLE+" limit 0,1";
			ResultSet rs = statement.executeQuery(sql);
			if(rs.next()){
				Introduction i = new Introduction();
				i.setID(rs.getString(ID));
				i.setImage_path(rs.getString(IMAGE_PATH));
				i.setContent(rs.getString(CONTENT));
				rs.close();
				return i;
				
			}else{
				rs.close();
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return null;
	}
}
